const db=require('../db/index')//导入数据库模块

const bcrypt=require('bcryptjs')
const jwt=require('jsonwebtoken')
const config=require('../config')
exports.regUser = (req, res) => {
    const userinfo=req.body
    const sqlStr='select *from ev_user  where username=?'
    db.query(sqlStr,userinfo.username,(err,results)=>{
        if(err){
            return res.cc(err)
        }
        if(results.length>0){
            return res.cc('用户名被占用')
        }
        userinfo.password=bcrypt.hashSync(userinfo.password,10)
        const sql='insert into ev_user set ?'
        db.query(sql,{username:userinfo.username,password:userinfo.password},(err,results)=>{
            if (err) {
                return res.cc(err)
                }
            if (results.affectedRows !== 1)return res.cc('注册失败，稍后再试')
            res.send({status:0,message:'注册成功'})
        })
    })
}
exports.login = (req, res) => {
    const userinfo=req.body;
    const sql='select * from ev_user where username=?'
    db.query(sql,userinfo.username,(err,results)=>{
        if(err)return res.cc(err)
        if(results.length!==1){
            return res.cc('登录失败')
        }
        const compareResult=bcrypt.compareSync(userinfo.password,results[0].password)
        if (!compareResult)return res.cc('登录失败')
        
        const user={...results[0],password:'',user_pic:''}
       const tokenStr=jwt.sign(user,config.jwtSecretKey,{expiresIn:config.expiresIn})
      res.send({
          status:0,
          message:'登录成功',
          token:'Bearer '+tokenStr
      })
    })
}